package com.rchat.pocmini.activity.groupsselect;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.airtalkee.sdk.AirtalkeeChannel;
import com.airtalkee.sdk.OnChannelListener;
import com.airtalkee.sdk.entity.AirChannel;
import com.airtalkee.sdk.entity.AirContact;
import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.activity.fragment.groups.GroupsPresenterImpl;
import com.rchat.pocmini.activity.groupsselect.view.GroupSelectBean;
import com.rchat.pocmini.bean2eventbus.UpdateGroupBean;
import com.rchat.pocmini.channel.PocAirtalkeeChannel;
import com.rchat.pocmini.global.AppGlobal;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by itsdf07 on 2017/8/31.
 */

public class GroupsSelectorPresenterImpl extends GroupsSelectorContract.Presenter implements OnChannelListener {

    /**
     * 标记界面返回的时候时候需要发出通知
     */
    private boolean isNeedNotify2Back = false;

    @Override
    void checkKeepGroups(ArrayList<GroupSelectBean> data) {
        mModel.checkKeepGroups(AirtalkeeAccount.getInstance().getUserId(), data, new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(BaseBean bean) {
                if (mView == null){
                    return;
                }
                mView.onBackToMain();
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null){
                    return;
                }
                mView.showNetError(bean.getDesc());
                ALog.d("result = %s,%s", bean.getCode(), bean.getDesc());
            }

            @Override
            public void onFinish() {

            }
        });
    }

    @Override
    void notifyUpdateGroups() {
        if (isNeedNotify2Back) {
            //调用频道界面刷新群组列表，此方式刷新后面还会修改
            UpdateGroupBean updateGroupBean = new UpdateGroupBean();
            updateGroupBean.setTarget(this.getClass(), GroupsPresenterImpl.class,"onEventBusUpdateGroups");
            updateGroupBean.setCurrentEvent(UpdateGroupBean.EVENT_GROPULIST_UPDATE);
            EventBus.getDefault().post(updateGroupBean);
        }
        mActivity.finish();
    }

    @Override
    public void onPStart() {
        super.onPStart();
        isNeedNotify2Back = mActivity.getIntent().getBooleanExtra(AppGlobal.KeyIntent.KEY_INTENT_UPDATE_GROUPS_BACK, true);
        ALog.d("isNeedNotify2Back = %s", isNeedNotify2Back);
        PocAirtalkeeChannel.getPocAirtalkeeChannelInstance().setOnChannelListener(this);
        PocAirtalkeeChannel.getPocAirtalkeeChannelInstance().getChannelList(AirtalkeeChannel.CHANNEL_TYPE_SECRET);
    }

    @Override
    public void onChannelListGet(boolean b, List<AirChannel> list) {
        ArrayList<GroupSelectBean> data = new ArrayList<>();
        String userId = AirtalkeeAccount.getInstance().getUserId();
        for (AirChannel airChannel : list) {
            if (!airChannel.getCreatorId().equals(userId)) {
                continue;
            }
            GroupSelectBean bean = new GroupSelectBean();
            bean.setGroupName(airChannel.getDisplayName());
            bean.setGroupId(airChannel.getId());
            bean.setSelectStatus(false);
            data.add(bean);
        }
        mView.updateList(data);
    }

    @Override
    public void onChannelMemberListGet(String s, List<AirContact> list) {

    }

    @Override
    public void onChannelOnlineCount(LinkedHashMap<String, Integer> linkedHashMap) {

    }

    @Override
    public void onChannelPersonalCreateNotify(AirChannel airChannel) {

    }

    @Override
    public void onChannelPersonalDeleteNotify(AirChannel airChannel) {

    }

    @Override
    public void onChannelMemberAppendNotify(AirChannel airChannel, List<AirContact> list) {

    }

    @Override
    public void onChannelMemberDeleteNotify(AirChannel airChannel, List<AirContact> list) {

    }

    @Override
    public void onChannelMemberUpdateNotify(AirChannel airChannel, List<AirContact> list) {

    }

    @Override
    public void onChannelMemberUpdateNotify(AirChannel airChannel, AirContact airContact) {

    }
}
